using UnityEngine;
using QFramework;
using UnityEngine.Video;
using System.Collections.Generic;
using System;

namespace ProjectSurvivor
{
	[Serializable]
	public class EnemyWaye
	{
		public float GenerateDuration = 1;
		public GameObject EnemyPrefab;

		public int Seconds = 10;
	}
	public partial class EnemyGenerator : ViewController
	{

		private float mCurrentGenerateSeconds = 0;
		private float mCurrentWaveSeconds = 0;

		public static BindableProperty<int> EnemyCount = new BindableProperty<int>(0);
		[SerializeField]
		public List<EnemyWaye> EnemyWayes = new List<EnemyWaye>();

		private Queue<EnemyWaye> mEnemyWavesQueue = new Queue<EnemyWaye>();

		public int waveCount = 0;
		public bool LastWave => waveCount == EnemyWayes.Count;

		public EnemyWaye CurrentWave => mCurrenyWave;

		void Start()
		{
			// Code Here
			foreach (var enemyWaye in EnemyWayes)
			{
				mEnemyWavesQueue.Enqueue(enemyWaye);
			}
		}
		public EnemyWaye mCurrenyWave = null;
		void Update()
		{
			if (mCurrenyWave == null)
			{
				if (mEnemyWavesQueue.Count > 0)
				{
					waveCount++;
					mCurrenyWave = mEnemyWavesQueue.Dequeue();
					mCurrentGenerateSeconds = 0;
					mCurrentWaveSeconds = 0;
				}
			}
			if (mCurrenyWave != null)
			{
				mCurrentGenerateSeconds += Time.deltaTime;
				mCurrentWaveSeconds += Time.deltaTime;

				if (mCurrentGenerateSeconds >= mCurrenyWave.GenerateDuration)
				{
					mCurrentGenerateSeconds = 0;
					var player = Player.Default;
					if (player)
					{
						var randomAngle = UnityEngine.Random.Range(0, 360f);
						var randomRadius = randomAngle * Mathf.Deg2Rad;
						var direction = new Vector3(Mathf.Cos(randomRadius), Mathf.Sin(randomRadius));
						var generatePos = player.transform.position + direction * 10;
						mCurrenyWave.EnemyPrefab.Instantiate().Position(generatePos).Show();
					}
				}
				if (mCurrentWaveSeconds >= mCurrenyWave.Seconds)
				{
					mCurrenyWave = null;
				}
			}
		}
	}
}
